package com.anxin.converter;

import com.anxin.entity.MedicalRecord;
import com.anxin.entity.excel.MedicalRecordExcel;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MedicalRecordConverter {
    
    public MedicalRecordExcel toExcel(MedicalRecord medicalRecord) {
        if (medicalRecord == null) {
            return null;
        }
        
        MedicalRecordExcel excel = new MedicalRecordExcel();
        excel.setId(medicalRecord.getId());
        excel.setPatientId(medicalRecord.getPatientId());
        excel.setDoctorId(medicalRecord.getDoctorId());
        excel.setDepartmentId(medicalRecord.getDepartmentId());
        excel.setAppointmentId(medicalRecord.getAppointmentId());
        excel.setVisitDate(Date.from(medicalRecord.getVisitDate().atStartOfDay(ZoneId.systemDefault()).toInstant()));
        excel.setChiefComplaint(medicalRecord.getChiefComplaint());
        excel.setPresentIllness(medicalRecord.getPresentIllness());
        excel.setPastHistory(medicalRecord.getPastHistory());
        excel.setPhysicalExamination(medicalRecord.getPhysicalExamination());
        excel.setAuxiliaryExamination(medicalRecord.getAuxiliaryExamination());
        excel.setDiagnosis(medicalRecord.getDiagnosis());
        excel.setTreatmentPlan(medicalRecord.getTreatmentPlan());
        excel.setFollowUpPlan(medicalRecord.getFollowUpPlan());
        excel.setCreateTime(Date.from(medicalRecord.getCreateTime().atZone(ZoneId.systemDefault()).toInstant()));
        excel.setUpdateTime(medicalRecord.getUpdateTime() != null ? 
            Date.from(medicalRecord.getUpdateTime().atZone(ZoneId.systemDefault()).toInstant()) : null);
        
        return excel;
    }
    
    public MedicalRecord toEntity(MedicalRecordExcel excel) {
        if (excel == null) {
            return null;
        }
        
        MedicalRecord medicalRecord = new MedicalRecord();
        medicalRecord.setId(excel.getId());
        medicalRecord.setPatientId(excel.getPatientId());
        medicalRecord.setDoctorId(excel.getDoctorId());
        medicalRecord.setDepartmentId(excel.getDepartmentId());
        medicalRecord.setAppointmentId(excel.getAppointmentId());
        medicalRecord.setVisitDate(excel.getVisitDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        medicalRecord.setChiefComplaint(excel.getChiefComplaint());
        medicalRecord.setPresentIllness(excel.getPresentIllness());
        medicalRecord.setPastHistory(excel.getPastHistory());
        medicalRecord.setPhysicalExamination(excel.getPhysicalExamination());
        medicalRecord.setAuxiliaryExamination(excel.getAuxiliaryExamination());
        medicalRecord.setDiagnosis(excel.getDiagnosis());
        medicalRecord.setTreatmentPlan(excel.getTreatmentPlan());
        medicalRecord.setFollowUpPlan(excel.getFollowUpPlan());
        medicalRecord.setCreateTime(excel.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        medicalRecord.setUpdateTime(excel.getUpdateTime() != null ? 
            excel.getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() : null);
        
        return medicalRecord;
    }
    
    public List<MedicalRecordExcel> toExcelList(List<MedicalRecord> medicalRecordList) {
        if (medicalRecordList == null) {
            return null;
        }
        
        return medicalRecordList.stream()
                .map(this::toExcel)
                .collect(Collectors.toList());
    }
    
    public List<MedicalRecord> toEntityList(List<MedicalRecordExcel> excelList) {
        if (excelList == null) {
            return null;
        }
        
        return excelList.stream()
                .map(this::toEntity)
                .collect(Collectors.toList());
    }
} 